﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.Web.Mvc.Security;
using TomNet.Web.Mvc;
using TomNet.IMSC.Model.DbModels.Organization;
using TomNet.Web.Mvc.UI;
using System.IO;
using System.Drawing.Imaging;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Web.Models;

namespace TomNet.IMSC.Web.Controllers
{
    public class IntermediaryController : BaseController
    {
        public IIntermediaryContract IntermediaryContract { get; set; }
        public IUsersContract UsersContract { get; set; }
        public IAgentCityContract AgentCityContract { get; set; }
        public IAreaContract AreaContract { get; set; }
        public IIntermediaryStaffContract IntermediaryStaffContract { get; set; }
        public IStaffContract StaffContract { get; set; }
        public IUsersRolesContract UsersRolesContract { get; set; }

        [Description("中介-列表")]
        //[GlobalAuthorization]
        public ActionResult Index()
        {
            return View();
        }

        [Description("中介-列表")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult IndexAsync(int pageIndex, int pageSize, string search = "")
        {
            //LoginModel login = CookiesManagement.GetLoginModel(CookiesManagement.GetTicket());
            //login.DepId
            var query = from m in IntermediaryContract.Entities.Where(x => !x.IsDeleted) select m;
            var list = from i in query
                       select new
                       {
                           id = i.Id,
                           Name = i.Name,
                           Address = i.Address,
                           Remark = i.Remark,
                           LicenceImage = i.LicenceImage,
                           AccountImage = i.AccountImage,
                           CreateTime = i.CreateTime,
                           LevelID = i.LevelID,
                           PublicAccountBank = i.PublicAccountBank,
                           PublicAccountUserName = i.PublicAccountUserName,
                           PublicAccountNumber = i.PublicAccountNumber,
                           PrivateAccountBank = i.PrivateAccountBank,
                           PrivateAccountUserName = i.PrivateAccountUserName,
                           PrivateAccountNumber = i.PrivateAccountNumber,
                           State = i.State,
                           AgentID = i.AgentID,
                           City = (from ac in AgentCityContract.Entities.Where(n => n.AgentID == i.AgentID)
                                   join a in AreaContract.Entities
                                   on ac.CityID equals a.Id
                                   select a.Name).FirstOrDefault(),
                           //房产业务员
                           HouseSalesman = (from isc in IntermediaryStaffContract.Entities.Where(o => o.IntermediaryID == i.Id && o.ModuleID == 1)
                                            join s in StaffContract.Entities.Where(e => !e.IsDeleted)
                                            on isc.StaffID equals s.Id
                                            select s.Name).FirstOrDefault(),
                           //装修业务员
                           RenovationSalesman = (from isc in IntermediaryStaffContract.Entities.Where(o => o.IntermediaryID == i.Id && o.ModuleID == 4)
                                                 join s in StaffContract.Entities.Where(e => !e.IsDeleted)
                                                 on isc.StaffID equals s.Id
                                                 select s.Name).FirstOrDefault(),
                           //金融业务员
                           FinanceSalesman = (from isc in IntermediaryStaffContract.Entities.Where(o => o.IntermediaryID == i.Id && o.ModuleID == 5)
                                              join s in StaffContract.Entities.Where(e => !e.IsDeleted)
                                              on isc.StaffID equals s.Id
                                              select s.Name).FirstOrDefault(),
                           CreatedTime = i.CreatedTime
                       };
            //if (login.Authority == 2)
            //{
            //    query = query.Where(x => x.DepId == login.DepId);
            //}

            //条件搜索
            if (!string.IsNullOrWhiteSpace(search))
            {
                list = list.Where(m => m.Name.Contains(search));
            }

            //var list = (from m in query
            //            select new UserShow
            //            {
            //                Id = m.Id,
            //                UserName = m.UserName,
            //                PassWord = m.PassWord,
            //                Phone = m.Phone,
            //                DepId = m.DepId,
            //                DepName = (from m1 in DepContract.Entities where m1.Id == m.DepId select m1.DepName).FirstOrDefault(),
            //                PositionName = (from m2 in PosContract.Entities where m2.Id == m.PositionId select m2.Name).FirstOrDefault(),// m.PositionId,
            //                Name = m.Name,
            //                Status = m.Status,
            //                No = m.No,
            //                IsLogin = m.IsLogin,
            //                Authority = m.Authority,
            //                IsLiability = m.IsLiability,
            //                CreatedTime = m.CreatedTime,
            //                IsDeleted = m.IsDeleted,
            //                IsLocked = m.IsLocked,
            //                AttSetName = (from n in AttSetContract.Entities where n.Id == m.AttSetId select n.Name).FirstOrDefault()
            //            });

            //数据总数
            var total = list.Count();

            //默认给一个排序的字段
            list = list.OrderByDescending(m => m.CreatedTime);

            //分页(假如total == 0，则取消分页查询，提高性能)
            list = total > 0 ? list.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : list;// null;

            //此处可以采用匿名对象 GridData<object>
            GridData<object> gridData = new GridData<object>() { Total = total, Rows = list.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }

        [Description("中介-查看")]
        //[GlobalAuthorization]
        public ActionResult IntermediaryInfo(int ID)
        {
            ViewBag.Model = IntermediaryContract.GetByKey(ID);
            ViewBag.UserName = UsersContract.Entities.FirstOrDefault(p => p.IntermediaryID == ID && UsersRolesContract.Entities.Count(m => m.UserID == p.Id && m.IntermediaryRoleID == 1) > 0).Name;
            ViewBag.UserPhone = UsersContract.Entities.FirstOrDefault(p => p.IntermediaryID == ID && UsersRolesContract.Entities.Count(m => m.UserID == p.Id && m.IntermediaryRoleID == 1) > 0).Phone;
            return View();
        }

        [Description("中介-添加")]
        //[GlobalAuthorization]
        public ActionResult Insert()
        {
            //if (mLogin.Authority == 3)
            //{
            //    Response.Redirect("/Error/Unauthorized");
            //    return View();
            //}

            //initDepartAndPosition(null);
            intorcount();
            return View();
        }

        [Description("中介-添加")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult InsertAsync(IntermediaryAndIntermediaryStaff json)
        {
            Intermediary entity = new Intermediary();
            entity.Name = json.Name;
            entity.Address = json.Address;
            entity.Remark = json.Remark;
            entity.PublicAccountBank = json.PublicAccountBank;
            entity.PublicAccountUserName = json.PublicAccountUserName;
            entity.PublicAccountNumber = json.PublicAccountNumber;
            entity.PrivateAccountBank = json.PrivateAccountBank;
            entity.PrivateAccountUserName = json.PrivateAccountUserName;
            entity.PrivateAccountNumber = json.PrivateAccountNumber;
            //所有AJAX的结果，返回统一数据格式
            var result = new AjaxResult();
            try
            {
                //entity.Authority = entity.RoleId;
                //entity.RoleId = 0;

                var query = IntermediaryContract.CheckExists(m => m.Name.Equals(entity.Name) && !m.IsDeleted);
                if (query)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "已经存在相同的中介";
                }
                else
                {
                    //如果选择部门不为空--默认加入考勤设置-----赵兴昌
                    //if (entity.DepId != 0)
                    //{
                    //    var dep = DepContract.GetByKey(entity.DepId);
                    //    entity.AttSetId = dep.AttSetId;
                    //}
                    //if (entity.Authority == 2) entity.IsLiability = true;

                    entity.CreateTime = DateTime.Now;
                    var cookies = Common.CookiesManagement.GetCookies();
                    var ticket = Common.CookiesManagement.GetTicket(cookies);
                    var model = Common.CookiesManagement.GetLoginModel(ticket);
                    entity.AgentID = model.AgentId;
                    var count = IntermediaryContract.Insert(entity);
                    if (count > 0)
                    {
                        //业务员添加
                        IntermediaryStaff staff = new IntermediaryStaff();
                        if (json.HouseSalesmanID != 0)
                        {
                            staff.IntermediaryID = entity.Id;
                            staff.ModuleID = 1;
                            staff.StaffID = json.HouseSalesmanID;
                            IntermediaryStaffContract.Insert(staff);
                        }
                        if (json.RenovationSalesmanID != 0)
                        {
                            staff.IntermediaryID = entity.Id;
                            staff.ModuleID = 4;
                            staff.StaffID = json.RenovationSalesmanID;
                            IntermediaryStaffContract.Insert(staff);
                        }
                        if (json.FinanceSalesmanID != 0)
                        {
                            staff.IntermediaryID = entity.Id;
                            staff.ModuleID = 5;
                            staff.StaffID = json.FinanceSalesmanID;
                            IntermediaryStaffContract.Insert(staff);
                        }

                        result.Type = AjaxResultType.Success;
                        result.Content = "录入成功";
                        //if (entity.DepId != 0 && entity.IsLiability)
                        //{
                        //    var newuser = Contract.Entities.Where(d => d.UserName == entity.UserName).FirstOrDefault();
                        //    UpdateIsLiability(entity.DepId, newuser.Id);//更新部门其他人员的部门负责人状态
                        //    UpdateAuthority(entity.DepId, newuser.Id);
                        //}

                        //UserInfoModel uiModel = new UserInfoModel();
                        //uiModel.Birthday = DateTime.Parse("1980-01-01");
                        //uiModel.IDNumber = "";//身份证号码
                        //uiModel.Address = "";
                        //uiModel.EMail = "";
                        //uiModel.Education = 0;
                        //uiModel.Nation = 0;//民族
                        //uiModel.PhotoId = 1;
                        //uiModel.Sex = 2;
                        //uiModel.UserId = (from m in Contract.Entities where m.UserName == entity.UserName select m.Id).FirstOrDefault();

                        //UserInfoContract.Insert(uiModel);

                    }
                    else
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "录入失败";
                    }
                }
            }
            catch //(Exception ex)
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }

        [Description("中介-修改")]
        //[GlobalAuthorization]
        public ActionResult Edit(int id)
        {
            //if (mLogin.Authority == 3)
            //{
            //    Response.Redirect("/Error/Unauthorized");
            //    return View();
            //}

            var entity = IntermediaryContract.GetByKey(id);
            //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
            if (entity == null)
            {
                throw new Exception("未找到该实体");
            }

            //initDepartAndPosition(entity);
            ViewData["entity"] = entity;
            var staff = IntermediaryStaffContract.Entities.Where(s => s.IntermediaryID == id);
            if (staff == null)
            {
                throw new Exception("未找到该实体");
            }
            var HouseSalesmanID = staff.Where(s => s.ModuleID == 1).Select(s => s.StaffID).FirstOrDefault();
            var RenovationSalesmanID = staff.Where(s => s.ModuleID == 4).Select(s => s.StaffID).FirstOrDefault();
            var FinanceSalesmanID = staff.Where(s => s.ModuleID == 5).Select(s => s.StaffID).FirstOrDefault();
            ViewBag.HouseSalesmanID = HouseSalesmanID;
            ViewBag.RenovationSalesmanID = RenovationSalesmanID;
            ViewBag.FinanceSalesmanID = FinanceSalesmanID;

            intorcount();
            return View();
        }

        [Description("中介-修改")]
        [HttpPost, AjaxOnly, NotCollected]
        public ActionResult EditAsync(IntermediaryAndIntermediaryStaff entity)
        {
            var result = new AjaxResult();
            try
            {
                //entity.Authority = entity.RoleId;
                //entity.RoleId = 0;

                var query = IntermediaryContract.CheckExists(m => m.Name.Equals(entity.Name) && m.Id != entity.Id);
                if (query)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "已经存在相同的中介";
                }
                else
                {
                    //业务员添加
                    IntermediaryStaff staff = new IntermediaryStaff();
                    var IntermediaryStaff = IntermediaryStaffContract.Entities.Where(m => m.IntermediaryID == entity.Id).ToList();
                    for (int i = 0; i < IntermediaryStaff.Count(); i++)
                    {
                        IntermediaryStaffContract.Delete(IntermediaryStaff[i].Id);
                    }
                    var houseCount = 0;
                    var renovationCount = 0;
                    var financeCount = 0;
                    if (entity.HouseSalesmanID != 0)
                    {
                        staff.IntermediaryID = entity.Id;
                        staff.ModuleID = 1;
                        staff.StaffID = entity.HouseSalesmanID;
                        houseCount = IntermediaryStaffContract.Insert(staff);
                    }
                    if (entity.RenovationSalesmanID != 0)
                    {
                        staff.IntermediaryID = entity.Id;
                        staff.ModuleID = 4;
                        staff.StaffID = entity.RenovationSalesmanID;
                        renovationCount = IntermediaryStaffContract.Insert(staff);
                    }
                    if (entity.FinanceSalesmanID != 0)
                    {
                        staff.IntermediaryID = entity.Id;
                        staff.ModuleID = 5;
                        staff.StaffID = entity.FinanceSalesmanID;
                        financeCount = IntermediaryStaffContract.Insert(staff);
                    }
                    //如果选择部门不为空--默认加入考勤设置-----赵兴昌
                    //if (entity.DepId != 0)
                    //{
                    //    var dep = DepContract.GetByKey(entity.DepId);
                    //    entity.AttSetId = dep.AttSetId;
                    //}
                    //if (entity.Authority == 2) entity.IsLiability = true;

                    //获取原纪录
                    var temp = IntermediaryContract.GetByKey(entity.Id);
                    //修改指定项
                    temp.Name = entity.Name;
                    temp.Address = entity.Address;
                    temp.PublicAccountBank = entity.PublicAccountBank;
                    temp.PrivateAccountBank = entity.PrivateAccountBank;
                    temp.PublicAccountUserName = entity.PublicAccountUserName;
                    temp.PrivateAccountUserName = entity.PrivateAccountUserName;
                    temp.PublicAccountNumber = entity.PublicAccountNumber;
                    temp.PrivateAccountNumber = entity.PrivateAccountNumber;
                    temp.Remark = entity.Remark;

                    var count = IntermediaryContract.Update(temp);
                    if (count > 0 || houseCount > 0 || renovationCount > 0 || financeCount > 0)
                    {
                        //if (entity.DepId != 0 && entity.IsLiability == true)
                        //{
                        //    UpdateIsLiability(entity.DepId, entity.Id);//更新部门其他人员的部门负责人状态
                        //    UpdateAuthority(entity.DepId, entity.Id);
                        //}

                        result.Type = AjaxResultType.Success;
                        result.Content = "修改成功";
                    }
                    else
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "修改失败";
                    }
                }
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }

        [Description("中介-查询")]
        [HttpPost, AjaxOnly]
        public ActionResult GetModelAsync(int id)
        {
            var result = new AjaxResult();

            try
            {
                var model = IntermediaryContract.GetByKey(id);

                if (model != null)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "操作成功";
                    result.Data = model;
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "获取失败";
                }
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }

            return JsonEx(result);
        }

        [Description("中介-删除")]
        [HttpPost, AjaxOnly]
        public ActionResult DeleteAsync(string ids)
        {
            var result = new AjaxResult();
            try
            {
                string[] idsStr = ids.Split(new char[] { ',' });
                int[] idsInt = Array.ConvertAll(idsStr, id => Convert.ToInt32(id));
                var count = IntermediaryContract.UpdateDirect(m => idsInt.Contains(m.Id) && UsersContract.Entities.Where(n => n.IntermediaryID == m.Id).Count() <= 0, m => new Intermediary { IsDeleted = true });

                if (count <= 0)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "中介含有经纪人，无法删除";
                }
                else if (count > 0 && count < idsInt.Length)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "部分中介含有经纪人，无法删除";
                }
                else
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "删除成功";
                }
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }

        [Description("中介-执照图片")]
        //[GlobalAuthorization]
        public ActionResult Upload(int id)
        {
            //if (mLogin.Authority == 3)
            //{
            //    Response.Redirect("/Error/Unauthorized");
            //    return View();
            //}

            var entity = IntermediaryContract.GetByKey(id);
            //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
            if (entity == null)
            {
                throw new Exception("未找到该实体");
            }

            //ViewBag.file_path = entity.LicenceImage;
            //ViewBag.file_name = Path.GetFileName(entity.LicenceImage);

            //initDepartAndPosition(entity);
            ViewData["entity"] = entity;
            return View();
        }

        [Description("中介-执照图片")]
        [HttpPost]
        public ActionResult UploadImageAsync()
        {
            //中介id
            int id = Convert.ToInt32(Request.QueryString["id"]);

            //文件流
            var oFile = Request.Files["file"];//.Files["txt_file"];
            var oStream = oFile.InputStream;
            //得到了文件的流对象，我们不管是用NPOI、GDI还是直接保存文件都不是问题了吧。。。。

            //文件名
            string m_saveName = "/Images/LicenceImages/";
            string m_savePath = Server.MapPath(m_saveName);
            string m_exName = Path.GetExtension(oFile.FileName);
            if (Directory.Exists(m_savePath) == false)
            {
                Directory.CreateDirectory(m_savePath);
            }
            string m_fileName = DateTime.Now.ToFileTime().ToString() + m_exName;

            //保存
            System.Drawing.Image img = System.Drawing.Image.FromStream(oStream);
            img.Save(m_savePath + m_fileName, ImageFormat.Jpeg);

            //// 生成临时图片-正方形的 
            //string n_file = "n_" + m_fileName;
            //ImagesHelper.imgCut(m_savePath + m_fileName, m_savePath + n_file, float.Parse(Request.Form["x"]), float.Parse(Request.Form["y"]), float.Parse(Request.Form["w"]), float.Parse(Request.Form["h"]));
            //System.IO.File.Delete(m_savePath + m_fileName);

            //string m_file = "m_" + m_fileName;
            ////-- 等比例缩放 生成新图片
            //ImagesHelper.GenerateHighThumbnail(m_savePath + n_file, m_savePath + m_file, 64, 64);
            //System.IO.File.Delete(m_savePath + n_file);

            //m_file = m_file.Replace(m_exName, "");//去掉扩展名
            //PhotoModel model = new PhotoModel()
            //{
            //    PhotoExName = oFile.FileName.Substring(oFile.FileName.LastIndexOf('.')),
            //    PhotoName = m_file,
            //    PhotoPath = m_saveName
            //};
            //PhotoContract.Insert(model);

            //var a = PhotoContract.Entities;
            //var lstOrderImport = new List<object>();
            //lstOrderImport.AddRange(PhotoContract.Entities.Where(x => x.PhotoName == m_file).ToList());


            //纪录图片路径
            int count = IntermediaryContract.UpdateDirect(m => m.Id.Equals(id), m => new Intermediary { LicenceImage = m_saveName + m_fileName });


            //----------------------------------
            var result = new AjaxResult();
            if (count > 0)
            {
                result.Type = AjaxResultType.Success;
                result.Content = "图片上传成功";
                //result.Data = lstOrderImport;
            }
            else
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }

        [Description("中介-执照图片")]
        //[GlobalAuthorization]
        public ActionResult View(int id)
        {
            //if (mLogin.Authority == 3)
            //{
            //    Response.Redirect("/Error/Unauthorized");
            //    return View();
            //}

            var entity = IntermediaryContract.GetByKey(id);
            //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
            if (entity == null)
            {
                throw new Exception("未找到该实体");
            }

            //ViewBag.file_path = entity.LicenceImage;
            //ViewBag.file_name = Path.GetFileName(entity.LicenceImage);

            //initDepartAndPosition(entity);
            ViewData["imgurl"] = entity.LicenceImage;
            return View();
        }

        [Description("中介-帐号图片")]
        //[GlobalAuthorization]
        public ActionResult Accounts(int id)
        {
            //if (mLogin.Authority == 3)
            //{
            //    Response.Redirect("/Error/Unauthorized");
            //    return View();
            //}

            var entity = IntermediaryContract.GetByKey(id);
            //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
            if (entity == null)
            {
                throw new Exception("未找到该实体");
            }

            //ViewBag.file_path = entity.LicenceImage;
            //ViewBag.file_name = Path.GetFileName(entity.LicenceImage);

            //initDepartAndPosition(entity);
            ViewData["entity"] = entity;
            return View();
        }

        [Description("中介-帐号图片")]
        [HttpPost]
        public ActionResult AccountsImageAsync()
        {
            //中介id
            int id = Convert.ToInt32(Request.QueryString["id"]);

            //文件流
            var oFile = Request.Files["file"];//.Files["txt_file"];
            var oStream = oFile.InputStream;
            //得到了文件的流对象，我们不管是用NPOI、GDI还是直接保存文件都不是问题了吧。。。。

            //文件名
            string m_saveName = "/Images/AccountsImages/";
            string m_savePath = Server.MapPath(m_saveName);
            string m_exName = Path.GetExtension(oFile.FileName);
            if (Directory.Exists(m_savePath) == false)
            {
                Directory.CreateDirectory(m_savePath);
            }
            string m_fileName = DateTime.Now.ToFileTime().ToString() + m_exName;

            //保存
            System.Drawing.Image img = System.Drawing.Image.FromStream(oStream);
            img.Save(m_savePath + m_fileName, ImageFormat.Jpeg);

            //// 生成临时图片-正方形的 
            //string n_file = "n_" + m_fileName;
            //ImagesHelper.imgCut(m_savePath + m_fileName, m_savePath + n_file, float.Parse(Request.Form["x"]), float.Parse(Request.Form["y"]), float.Parse(Request.Form["w"]), float.Parse(Request.Form["h"]));
            //System.IO.File.Delete(m_savePath + m_fileName);

            //string m_file = "m_" + m_fileName;
            ////-- 等比例缩放 生成新图片
            //ImagesHelper.GenerateHighThumbnail(m_savePath + n_file, m_savePath + m_file, 64, 64);
            //System.IO.File.Delete(m_savePath + n_file);

            //m_file = m_file.Replace(m_exName, "");//去掉扩展名
            //PhotoModel model = new PhotoModel()
            //{
            //    PhotoExName = oFile.FileName.Substring(oFile.FileName.LastIndexOf('.')),
            //    PhotoName = m_file,
            //    PhotoPath = m_saveName
            //};
            //PhotoContract.Insert(model);

            //var a = PhotoContract.Entities;
            //var lstOrderImport = new List<object>();
            //lstOrderImport.AddRange(PhotoContract.Entities.Where(x => x.PhotoName == m_file).ToList());


            //纪录图片路径
            int count = IntermediaryContract.UpdateDirect(m => m.Id.Equals(id), m => new Intermediary { AccountImage = m_saveName + m_fileName });


            //----------------------------------
            var result = new AjaxResult();
            if (count > 0)
            {
                result.Type = AjaxResultType.Success;
                result.Content = "图片上传成功";
                //result.Data = lstOrderImport;
            }
            else
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }

        [Description("中介-帐号图片")]
        //[GlobalAuthorization]
        public ActionResult AccountsView(int id)
        {
            //if (mLogin.Authority == 3)
            //{
            //    Response.Redirect("/Error/Unauthorized");
            //    return View();
            //}

            var entity = IntermediaryContract.GetByKey(id);
            //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
            if (entity == null)
            {
                throw new Exception("未找到该实体");
            }

            //ViewBag.file_path = entity.LicenceImage;
            //ViewBag.file_name = Path.GetFileName(entity.LicenceImage);

            //initDepartAndPosition(entity);
            ViewData["imgurl"] = entity.AccountImage;
            return View();
        }

        [Description("对公账户")]
        public ActionResult PublicAccount(int id)
        {
            var entity = IntermediaryContract.Entities.Where(m => m.Id == id).FirstOrDefault();
            if (entity == null) { throw new Exception("未找到该实体"); }
            var PublicAccountBank = entity.PublicAccountBank;
            var PublicAccountUserName = entity.PublicAccountUserName;
            var PublicAccountNumber = entity.PublicAccountNumber;
            if (entity.PublicAccountBank == null)
            {
                PublicAccountBank = "";
            }
            if (entity.PublicAccountUserName == null)
            {
                PublicAccountUserName = "";
            }
            if (entity.PublicAccountNumber == null)
            {
                PublicAccountNumber = "";

            }
            ViewBag.PublicAccountBank = PublicAccountBank;
            ViewBag.PublicAccountUserName = PublicAccountUserName;
            ViewBag.PublicAccountNumber = PublicAccountNumber;
            return View();
        }

        [Description("对私账户")]
        public ActionResult PriviceAccount(int id)
        {
            var entity = IntermediaryContract.Entities.Where(m => m.Id == id).FirstOrDefault();
            if (entity == null) { throw new Exception("未找到该实体"); }
            var PrivateAccountBank = entity.PrivateAccountBank;
            var PrivateAccountUserName = entity.PrivateAccountUserName;
            var PrivateAccountNumber = entity.PrivateAccountNumber;
            if (entity.PrivateAccountBank == null)
            {
                PrivateAccountBank = "";
            }
            if (entity.PrivateAccountUserName == null)
            {
                PrivateAccountUserName = "";
            }
            if (entity.PrivateAccountNumber == null)
            {
                PrivateAccountNumber = "";

            }
            ViewBag.PrivateAccountBank = PrivateAccountBank;
            ViewBag.PrivateAccountUserName = PrivateAccountUserName;
            ViewBag.PrivateAccountNumber = PrivateAccountNumber;
            return View();
        }
        #region 房产业务员，装修业务员，金融业务员列表
        public void intorcount()
        {
            List<SelectListItem> gridData = new List<SelectListItem>();
            gridData.Add(new SelectListItem { Value = "0", Text = "无" });
            var data = from staff in StaffContract.TrackEntities.Where(m => !m.IsDeleted)
                       select new SelectListItem
                       {
                           Value = staff.Id + "",
                           Text = staff.Name
                       };
            gridData.AddRange(data);
            ViewData["ientity"] = new SelectList(gridData);
        }
        #endregion 
    }
}